package com.shj.service.impl;

import com.shj.dao.SysUserRoleMapper;
import com.shj.entity.SysRole;
import com.shj.entity.SysUserRole;
import com.shj.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description 用户角色
 * @Author songhuajian
 * @Date 2020/1/12 23:08
 **/
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * @description 根据用户编号查询角色
     * @author songhuajian
     * @date 2020/1/12 23:19
     */
    @Override
    public List<SysUserRole> findByUserId(String userId){
        return sysUserRoleMapper.selectByUserId(userId);
    }

    /**
     * @Description 添加用户角色
     * @author songhuajian
     * @date 2020/5/2 16:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(String userId, List<String> roles) {
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(userId);
        for (String roleId : roles) {
            sysUserRole.setRoleId(roleId);
            sysUserRoleMapper.insert(sysUserRole);
        }
    }

    /**
     * @Description 删除用户角色
     * @author songhuajian
     * @date 2020/5/2 16:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String userId) {
        sysUserRoleMapper.deleteByUserId(userId);
    }
}
